package com.zhs.gccs.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.annimon.stream.Stream;
import com.zhs.gccs.R;
import com.zhs.gccs.model.GccsOfProject;
import com.zhs.gccs.model.XzqArea;
import com.zhs.gccs.utils.OnNoDoubleClickListener;
import com.zhs.gccs.utils.ScreenUtils;
import com.zhs.gccs.utils.ToastUtils;
import com.zhs.gccs.view.gt.XzqItemAdapter;

import java.util.List;

/**
 * Created by chengzf on 2024/7/22.
 */
public class Xzq1Dialog {
    public interface OnSelectedListener {
        void onSelected(String district);
    }

    public static class Builder extends BaseDialog.Builder<Builder> {
        List<GccsOfProject> list;
        private View tv_cancel;
        private View tv_confirm;
        private List<XzqArea> xzqAreas;
//        private RecyclerView rv_items_1;
//        private List<XzqArea> xzq1Areas;
//        XzqItemAdapter items_1Adapter;
//        private String province;
        private RecyclerView rv_items_3;
        private List<XzqArea> xzq3Areas;
        XzqItemAdapter items_3Adapter;
        private String district;

        public Builder(Context context,
                       String topXzq, String d,
                       List<XzqArea> xzqAreas,
                       OnSelectedListener listener) {
            super(context);
            this.xzqAreas = xzqAreas;
            this.district = d;
            setContentView(R.layout.view_single_district_select3);
            setAnimStyle(BaseDialog.ANIM_IOS);
            setGravity(Gravity.BOTTOM);
            setBackgroundDimEnabled(true);
            setCancelable(true);
            setCanceledOnTouchOutside(true);
            setWidth((int) (ScreenUtils.dpToPx(context, ScreenUtils.getScreenWidthDp(context)) * 1));
            setHeight((int) (ScreenUtils.getScreenHeightPx(context) * 0.6));
            rv_items_3 = findViewById(R.id.rv_items_3);
            xzq3Areas = Stream.of(xzqAreas).filter(i->i.getXzqhpdm().equals(topXzq)).toList();
            items_3Adapter = new XzqItemAdapter(context,xzq3Areas);
            rv_items_3.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
            rv_items_3.setAdapter(items_3Adapter);
            items_3Adapter.setOperationListener(new XzqItemAdapter.OperationListener() {
                @Override
                public void onItemClick(int position) {
                    district = xzq3Areas.get(position).getXzqhdm();
                    Stream.of(xzq3Areas).forEach(i->i.setChecked(false));
                    xzq3Areas.get(position).setChecked(true);
                    items_3Adapter.notifyDataSetChanged();
                }
            });
            if(district!=null){
                XzqArea dArea = Stream.of(xzq3Areas).
                        filter(i -> i.getXzqhdm().equals(district)).findFirstOrElse(null);
                if(dArea!=null){
                    dArea.setChecked(true);
                }
                items_3Adapter.notifyDataSetChanged();
            }

            tv_cancel = findViewById(R.id.tv_cancel);
            tv_cancel.setOnClickListener(new OnNoDoubleClickListener() {
                @Override
                protected void onNoDoubleClick(View v) {
                    Stream.of(list).forEach(item->item.setSelected(false));
                    dismiss();
                    if(listener!=null){
                        listener.onSelected(district);
                    }
                }
            });
            tv_confirm = findViewById(R.id.tv_confirm);
            tv_confirm.setOnClickListener(new OnNoDoubleClickListener() {
                @Override
                protected void onNoDoubleClick(View v) {
                    if(xzq3Areas!=null&&xzq3Areas.size()>0&& TextUtils.isEmpty(district)){
                        ToastUtils.toast("请选择区县");
                        return;
                    }
                    dismiss();
                    if(listener!=null){
                        listener.onSelected(district);
                    }
                }
            });
        }
    }
}
